package com.lh.blog.controller;

import com.lh.blog.domain.entity.ResponseResult;
import com.lh.blog.domain.entity.User;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Hang Luo
 * @Date: 2023/7/5-12:13
 * @Descript:
 **/
@Controller
public class PageController {
    @RequestMapping("/")
    public String index() {
        return "index.html";
    }

    @ResponseBody
    @GetMapping("/userList")
    public @NotNull ResponseResult<Object> getUsers() {
        ResponseResult<Object> res = ResponseResult.ResponseResultSuccess();
        Map<String, Object> map = new HashMap<>(4);
        map.put("user", new Object());
        map.put("article", new Object());
        map.put("userList", new ArrayList<Object>());
        map.put("pageSize", 10);
        map.put("currPage", 1);
        res.setData(map);
        return res;
    }

    @GetMapping("/user")
    public @NotNull ResponseResult<Object> getUser() {
        ResponseResult<Object> res = ResponseResult.ResponseResultSuccess();
        User user = new User();
        res.setData(user);
        return res;
    }

    @PostMapping("/upload")
    @ResponseBody
    public Map<String, Object> uploadImage(@RequestParam("file") @NotNull MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        // 处理上传的图片文件
        // 可以使用file.getInputStream()获取文件输入流，进行进一步的操作
        // 返回适当的响应或处理结果
        Map<String, Object> res = new HashMap<>();
        res.put("errno", 0);

        HashMap[] images = new HashMap[1];
        HashMap<String, Object> image = new HashMap<>();
        image.put("url", "https://psstatic.cdn.bcebos.com/video/wiseindex/aa6eef91f8b5b1a33b454c401_1660835115000.png");
        image.put("alt", "百度一下");
        image.put("href", "https://psstatic.cdn.bcebos.com/video/wiseindex/aa6eef91f8b5b1a33b454c401_1660835115000.png");
        images[0] = image;

        res.put("data", images);
        return res;
    }
}
